﻿@model IEnumerable<Noodles.Core.Model.BlogPost>

 
    @foreach (var item in Model)
    {
        <h2>          
        @item.Title 
        </h2>
          
       <p>@item.Description</p>   
       <p>
       @Html.Raw(HttpUtility.HtmlDecode(item.PostData))
       
       </p>   
            <br /> 
                <div class="display-label">Comments</div>
                <div class="display-field">                    
                        @Html.Action("Comments", new { BlogID = item.BlogID })
                    </div>        
       
                    <div class="display-label">Add Comment</div>
                    <div class="display-field">
                    <input type="hidden" id="hdnBlogID" value="@item.BlogID" />
                    @if (item.IsCommentable)
                    {
                        @Html.Partial("CommentForm")
                    }
                    </div>           
    }
            
        <script type='text/javascript'>
            $(".buttonSubmit").click(function (e) {
                e.preventDefault();
                if (validateName() & validateEmail() & validateMessage()) {

                    var myJSONObject = { "Comments": [{ "BlogID": "1", "UserEmail": "newURI", "UserName": "hello"}] };
                    //
                    //  e.preventDefault();


                    var person = getPerson();
                    //   var json = $.toJSON(person);
                    var json = JSON.stringify(person);


                    $.ajax({
                        type: "POST",
                        url: '@Url.Action("Create","Comments")',
                        dataType: "json",
                        contentType: "application/json; charset=utf-8",
                        data: json,
                        beforeSend: function () {
                            //show loader...
                            $("#divWait").show();
                        },
                        complete: function () {
                            // hide loader ...
                            $("#divWait").hide();
                            $(".buttonSubmit").val("Comment Submitted !");
                            $(".buttonSubmit").attr("disabled", "disabled");
                        },
                        error: function (error) {
                            alert("There was an error posting the data to the server !");
                        }
                    });
                }
                else {
                    return false;
                }

            });
                function getPerson() {
                    var blogID = $("#hdnBlogID").val();
                    var userEmail = $("#email").val();
                    var userName = $("#username").val();
                    var website = $("#website").val();
                    var commentNew = $("#comment").val();                  
                 
                    return (blogID == "") ? null : { BlogID: blogID, UserEmail: userEmail, UserName: userName, UserData: website ,Comment:commentNew};
                }
               
        </script>
          

